package com.xywy.bloodandroid.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.bloodandroid.MyApplication;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.util.DeviceUtil;
import com.xywy.bloodandroid.util.NetUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.BaseRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

/**
 ****************************************** 
 * @Title: BaseAct.java
 * @Package com.xywy.bloodandroid.base
 * @Description: TODO(activity�Ļ���)
 * @author smz
 * @date 2014-12-23 ����10:21:21
 * @version V1.0
 ******************************************* 
 */
public abstract class BaseActivity extends Activity implements OnClickListener {

	public static int deviceWidth = 0;
	public static int deviceHeight = 0;

	public static Context context;
	private ImageView left_img;
	private TextView middle_title;
	private LinearLayout tobar;
	private ImageView right_img;
	protected ProgressDialog waittingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		MyApplication.getInstance().addActivity(this);
		initBase();
		initView(savedInstanceState);
		initListener();
		initData();
		MyApplication.getInstance().addActivity(this);
	}

	private void initBase() {
		context = this;
		if (deviceWidth == 0)
			deviceWidth = DeviceUtil.getDeviceWidth(this);
		if (deviceHeight == 0)
			deviceHeight = DeviceUtil.getDeviceHeight(this);

	}

	protected abstract void initView(Bundle savedInstanceState);

	protected abstract void initListener();

	protected abstract void initData();

	public void waitingDialog(Context context, String message) {
		if (waittingDialog == null)
			waittingDialog = new ProgressDialog(context);
		waittingDialog.setCanceledOnTouchOutside(false);// �����಻��ʧ
		waittingDialog.setMessage(message);
		waittingDialog.show();
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	protected boolean executeRequest(BaseRequest<?> request) {
		if (NetUtil.hasNetwork(context)) {
			waitingDialog(this, getResources().getString(R.string.request));
			VolleyManager.addRequest(request, this);
			return true;
		} else {
			UIUtil.showToast(context,
					getResources().getString(R.string.not_network));
			UIUtil.DissDialog(this, waittingDialog);
			return false;
		}
	}

	/**
	 * 不懈怠参数
	 * 
	 * @param clazz
	 */
	protected void enterActivity(Class<?> clazz) {
		startActivity(new Intent(this, clazz));
	}
	@Override
	protected void onStop() {
		super.onStop();
		VolleyManager.cancelRequest(this);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		VolleyManager.cancelRequest(this);
	}

	/**
	 * 携带参数
	 * 
	 * @param clazz
	 * @param bundle
	 */
	protected void enterActivity(Class<?> clazz, Bundle bundle) {
		Intent intent = new Intent(this, clazz);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void onClick(View arg0) {

	}

	public void showToast(String content) {
		// 搞笑
		// User.getInstance(this).setUser(new User());
		Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
	}

}
